﻿using Microsoft.PointOfService;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace POS
{
    class PrintReceiptClass
    {
        static PosPrinter PrinterUsed;
        public static int RecLineChars = 40;
        public static int receiptNo = 20461;

        public static void ConnectToPrinter(PosPrinter printer)
        {
            PrinterUsed = printer;
            try
            {

                PrinterUsed.Open();
                PrinterUsed.Claim(10000);
                PrinterUsed.DeviceEnabled = true;


                //MessageBox.Show("Connected to Printer");
            }
            catch
            {
                MessageBox.Show("NOT ABLE TO Connected to Printer");
            }
        }

        public static void DisconnectFromPrinter(PosPrinter printer)
        {
            printer.Release();
            //printer.Close();
        }

        public static void PrintTextLine(PosPrinter printer, string text)
        {
            //MessageBox.Show(printer.RecLineChars.ToString());
            if (text.Length < RecLineChars)
                printer.PrintNormal(PrinterStation.Receipt, text + Environment.NewLine); //Print text, then a new line character.
            else if (text.Length > RecLineChars)
                printer.PrintNormal(PrinterStation.Receipt, TruncateAt(text, RecLineChars) + Environment.NewLine); //Print exactly as many characters as the printer allows, truncating the rest, no new line character (printer will probably auto-feed for us)
            else if (text.Length == RecLineChars)
                printer.PrintNormal(PrinterStation.Receipt, text + Environment.NewLine); //Print text, no new line character, printer will probably auto-feed for us.
        }

        public static string TruncateAt(string text, int maxWidth)
        {
            string retVal = text;
            if (text.Length > maxWidth)
                retVal = text.Substring(0, maxWidth);

            return retVal;
        }

        public static void PrintReceiptHeader(PosPrinter printer)
        {
            string companyName = "               Hayashi Japanese Steakhouse";
            string addressLine1 = "                     4220 82nd St";
            string addressLine2 = "                  Lubbock, TX 79423";
            string PhoneNumber = "                    (806) 792-6868";
            DateTime dateTime = DateTime.Now;
            string ServerName = "Roger Jenkins";

            char pad = '.';
            PrintTextLine(printer, companyName);
            PrintTextLine(printer, addressLine1);
            PrintTextLine(printer, addressLine2);
            PrintTextLine(printer, PhoneNumber);
            PrintTextLine(printer, new string('=', RecLineChars));
            //PrintTextLine(printer, String.Format("DATE : {0}", dateTime.ToShortDateString()));
            //PrintTextLine(printer, String.Format("SERVER : {0}", ServerName));


            //PrintTextLine(printer, string.Empty);
            PrintTextLine(printer, string.Empty);
            PrintTextLine(printer, string.Empty);


        }

        public static void PrintLineItem(PosPrinter printer, int Qty, string Description, double Amount)
        {

        }

        public static string Truncate(string source, int length)
        {
            if (source.Length > length)
            {
                source = source.Substring(0, length);
            }
            return source;
        }

        public static void PrintCreditCardSlip(PosPrinter printer,string ServerName, double Total)
        {
            string companyName = "               Hayashi Japanese Steakhouse";
            string addressLine1 = "                     4220 82nd St";
            string addressLine2 = "                  Lubbock, TX 79423";
            string PhoneNumber = "                    (806) 792-6868";
            DateTime dateTime = DateTime.Now;
            //string ServerName = "Roger Jenkins";

            char pad = '.';
            PrintTextLine(printer, companyName);
            PrintTextLine(printer, addressLine1);
            PrintTextLine(printer, addressLine2);
            PrintTextLine(printer, PhoneNumber);
            PrintTextLine(printer, new string('=', RecLineChars));
            PrintTextLine(printer, string.Empty);
            PrintTextLine(printer, string.Empty);

            string totalLine = Total.ToString("C");
            string totalcharded = "     TOTAL CHARGED:           " + totalLine;
            string TipLine =      "     TIP:                          ______________";
            string line =         "     ------------------------------------------------------------------------------";

            PrintTextLine(printer, totalcharded);

            PrintTextLine(printer, string.Empty);
            PrintTextLine(printer, string.Empty);

            //string TipLine = " TIP:  ______________";

            PrintTextLine(printer, TipLine);
            PrintTextLine(printer, string.Empty);
            PrintTextLine(printer, line);


        }
    }
}
